<?php

namespace Home\Controller;

use Home\Model\ShelvesViewModel;
use Home\Model\UTagModel;
use Home\Org\Util\Sqlserver;

/**
 * 上架
 * @author dem
 *
 */
class ShelvesController extends HomeController
{


    function index()
    {
        echo 'index';
        exit;
    }

    /**
     * 上架通知
     */
    function createlist()
    {
        $this->display();
    }
    function shelvesNotice()
    {
        $this->display();
    }

    function getMylist()
    {
        $data = ShelvesViewModel::getListByemp();
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit;
    }

    function getShelvesDetailed()
    {
        $pid = I('pid');
        echo json_encode(ShelvesViewModel::getShelvesDetailed($pid), JSON_UNESCAPED_UNICODE);
        exit;
    }


    function saveshelvesList()
    {
        $respons = array('code' => 0, 'data' => null);
        $data = json_decode(I('savedata'), 1);

        $respons['code'] = ShelvesViewModel::saveshelvesList($data);
        if ($respons['code']) {
            $respons['data'] = ShelvesViewModel::getListByemp();
        }
        echo json_encode($respons, JSON_UNESCAPED_UNICODE);
        exit;
    }

    function saveShelvesDetailed()
    {
        $data = json_decode(I('data'), 1);
        $pid = I('pid');
        echo ShelvesViewModel::saveShelvesDetailed($data, $pid);
    }

    function updateShelvesDetailed()
    {
        $data = json_decode(I('data'), 1);
        echo ShelvesViewModel::updateShelvesDetailed($data);
    }
    function updateShelvesDetailedOnlyMallPrice()
    {
        $data = json_decode(I('data'), 1);
        echo ShelvesViewModel::updateShelvesDetailedOnlyMallPrice($data);
    }

    function updateShelvesDetailedRemarkForXiaJia()
    {
        $data = json_decode(I('data'), 1);

        if ($data['_state'] == "modified")
        echo ShelvesViewModel::updateShelvesDetailedRemarkForXiaJia($data);
    }

    function delShelvesDetailed()
    {
        $ids = I('ids');
        $ids = rtrim($ids, ',');

        echo ShelvesViewModel::delShelvesDetailed($ids);
    }

    function select()
    {
        $this->display();
    }


    function getSelectData()
    {
        $key = I('key');
        require 'Application/Home/ORG/Util/Sqlserver.class.php';
        $sql = "select CardCode as U_Cardcode ,ShortName as U_CardName ,CManager from AX_ChannelList where  CardCode like '%{$key}%' union all select CardCode,ShortName,CManager from AX_ChannelList where  ShortName like '%{$key}%'";
        if (!$key) {
            $sql = "select CardCode as U_Cardcode  ,ShortName as U_CardName ,CManager from AX_ChannelList  ";

        }
        $sql = iconv("utf-8", "gb2312", $sql);
        $db = new Sqlserver();
        $db->connent();
        $data = $db->query($sql);

        $array = array();
        foreach ($data as $item) {
            array_push($array, $item);
        }


        $data = array('total' => count($data), 'data' => $array);
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    function sendnotice()
    {
        $respons = array('code' => 0, 'msg' => 'ok');
        $id = I('id');
        $userlist = I('userlist');
        $listRemark = I('listRemark');

        $data = ShelvesViewModel::getListById($id);
        if (!$data) {
            echo false;
            exit;
        }

        ShelvesViewModel::updateIs_send($id,$userlist,$listRemark);//修改发送状态

        $listModel = M('ShelvesList');
        $listInfo = $listModel->where("id = {$id}")->find();
        $listName = $listInfo['name'];
        $issueUser = $listInfo['emp_no'];
        $pingTai = $listInfo['u_code'];
        $pingTaiName = $listInfo['u_name'];

        //TODO: 发送邮件微信
        $toEmpArray = explode(',',$userlist);
        $empArrayWithDomain = array();
        foreach ($toEmpArray as $empKey => $empValue)
        {
            $empArrayWithDomain[] = $empValue.'@g-town.com.cn';
        }
        $this->sendEmail($empArrayWithDomain[0], "上架清单通知 - 清单名:{$listName},平台:{$pingTai},{$pingTaiName}", "上架清单:{$listName}<br>平台:{$pingTai},{$pingTaiName}<br/>产品经理:{$issueUser}",'',$empArrayWithDomain);

        //foreach ($toEmpArray as $empKey => $empValue)
        //{
            //$this->sendEmail($empValue.'@g-town.com.cn', "上架清单通知 - 清单名:{$listName},平台:{$pingTai}", "上架清单:{$listName}<br>平台:{$pingTai}<br/>产品经理:{$issueUser}");
            //weixinsendmsg("有新的上架清单通知", "清单名:{$listName}\n请及时登陆OA系统处理",$issueUser);
        //}

        $res = array(
            'code' => 0,
            'msg' => '通知成功!'
        );

        echo json_encode($res);
        exit;
    }

    function copyshelveslist()
    {
        $listID = $_REQUEST['listid'];
        $uCode = $_REQUEST['u_code'];
        $uText = $_REQUEST['u_text'];
        $listType = $_REQUEST['listtype'];
        $newListName = $_REQUEST['newlistname'];

        $emp_no = $_SESSION['emp_no'];

        $shelvesListModel = M('ShelvesList');
        $result = $shelvesListModel->where("id={$listID} and emp_no='{$emp_no}'")->find();
        // 已发通知的清单
        if(1)//if ($result['is_send']==1)
        {
            // 创建新的清单
            $ctime = date('Y-m-d H:i:s');
            $listSaveData = array(
                'name' => $newListName,
                'type' => $listType,
                'emp_no' => $emp_no,
                'ctime' => $ctime,
                'u_code' => $uCode,
                'u_name' => $uText,
                'is_send' => 0,
            );
            $newListID = $shelvesListModel->add($listSaveData);

            // 复制旧清单的内容到新清单中
            $detailModel = M('ShelvesDetailed');
            $data = $detailModel->where("pid='{$listID}'")->select();
            foreach ($data as $key => $value){
                $value['pid'] = $newListID;
                $value['status'] = 0;
                unset($value['id']);
                $detailModel->add($value);
            }

            $respons['code'] = $newListID;
            if ($respons['code']) {
                $respons['data'] = ShelvesViewModel::getListByemp();
            }
            echo json_encode($respons, JSON_UNESCAPED_UNICODE);
            exit;

        }
        else{
            echo "只能复制已通知上架的清单";
        }

    }

    function returnListType($type){
        $returnValue = "";
        switch ($type) {
            case '0':
                $returnValue = "普通";
                break;
            case '1':
                $returnValue = "活动";
                break;
            case '2':
                $returnValue = "集采";
                break;
        }
        return $returnValue;
    }

    function returnItemStatus($type){
        $returnValue = "";
        switch ($type)
        {
            case '0':
                $returnValue = "默认";
                break;
            case '1':
                $returnValue = "通知发出";
                break;
            case '2':
                $returnValue = "提报";
                break;
            case '3':
                $returnValue = "上架";
                break;
            case '4':
                $returnValue = "下架";
                break;
        }
        return $returnValue;
    }

    function noticepdept()
    {
        $listID = $_REQUEST['listid'];
        $listName = $_REQUEST['listname'];
        $code = $_REQUEST['code'];
        $emp_no = $_SESSION['emp_no'];

        $listModel = M('ShelvesList');
        $data = $listModel->where("name = '{$listName}'")->find();
        $pingTai = $data['u_code'];
        $issuerPM = $data['emp_no'];
        //var_dump($data['emp_no']);die();

        //TODO: 发送邮件微信
        $this->sendEmail($issuerPM.'@g-town.com.cn', "上架清单驳回通知 - 清单名:{$listName},平台:{$pingTai}", "上架清单:{$listName}<br>平台:{$pingTai}<br/>产品经理:{$issuerPM}<br/>驳回操作执行人:{$emp_no}");
        //weixinsendmsg("上架清单驳回通知", "清单名:{$listName}\n请及时登陆OA系统处理",$emp_no);

        $res = array(
            'code' => 0,
            'msg' => '通知成功!'
        );

        echo json_encode($res);
        exit;
    }

    function exportexcelshelveslistforsales()
    {
        $listID = $_REQUEST['listid'];
        $listName = $_REQUEST['listname'];
        $code = $_REQUEST['code'];
        $emp_no = $_SESSION['emp_no'];

//        $shelvesListModel = M('ShelvesList');
//        $result = $shelvesListModel->where("id={$listID} and emp_no='{$emp_no}'")->find();
        if(1)//if ($result['is_send']==1)
        {

            //$shelvesType = $result['type'];
            //$shelvesName = $result['name'];

            $detailModel = M('ShelvesDetailed');
            //$data = $detailModel->where("pid='{$listID}'")->select();

            $quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.*, sapitem.logo from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on detailed.oa_id = sapitem.item_id where list.is_send=1 and list.u_code = '{$code}' and list.name like '%{$listName}%' and list.to_emp like '%{$emp_no}%'";
            $data = $detailModel->query($quersql);

            //Export to Excel:

            Vendor('Excel.PHPExcel');
            $objPHPExcel = new \PHPExcel();
            $currentSheet = $objPHPExcel->getSheet(0);
            $m = 1;
            $currentSheet->setCellValue('A'.$m, 'OA物料号');
            $currentSheet->setCellValue('B'.$m, '上架类型');
            $currentSheet->setCellValue('C'.$m, '上架清单名');
            $currentSheet->setCellValue('D'.$m, '产品经理');
            $currentSheet->setCellValue('E'.$m, 'SAP物料号');

            $currentSheet->setCellValue('F'.$m, 'SAP物料名称');
            $currentSheet->setCellValue('G'.$m, '规格');
            //$currentSheet->setCellValue('H'.$m, '平台商品代码');
            $currentSheet->setCellValue('H'.$m, '数量');
            $currentSheet->setCellValue('I'.$m, '添加时间');
            $currentSheet->setCellValue('J'.$m, '主品物料号');

            $currentSheet->setCellValue('K'.$m, '颜色');
            $currentSheet->setCellValue('L'.$m, '毛利');
            $currentSheet->setCellValue('M'.$m, '商城价');
            $currentSheet->setCellValue('N'.$m, '毛利率');
            $currentSheet->setCellValue('O'.$m, '采购价');

            $currentSheet->setCellValue('P'.$m, '分期数');
            $currentSheet->setCellValue('Q'.$m, '分期费率(%)');
            $currentSheet->setCellValue('R'.$m, '预估运费');
            $currentSheet->setCellValue('S'.$m, '赠品价格');
            $currentSheet->setCellValue('T'.$m, '佣金');

            $currentSheet->setCellValue('U'.$m, '增值税等17%');
            $currentSheet->setCellValue('V'.$m, '包装费');
            $currentSheet->setCellValue('W'.$m, '返利金额');

            // Attachment begin
            $currentSheet->setCellValue('X'.$m, '天猫价');
            $currentSheet->setCellValue('Y'.$m, '天猫销量');
            $currentSheet->setCellValue('Z'.$m, '天猫');

            $currentSheet->setCellValue('AA'.$m, '京东价');
            $currentSheet->setCellValue('AB'.$m, '京东销量');
            $currentSheet->setCellValue('AC'.$m, '京东');

            $currentSheet->setCellValue('AD'.$m, '广发价');
            $currentSheet->setCellValue('AE'.$m, '广发销量');
            $currentSheet->setCellValue('AF'.$m, '广发');

            $currentSheet->setCellValue('AG'.$m, '建行价');
            $currentSheet->setCellValue('AH'.$m, '建行销量');
            $currentSheet->setCellValue('AI'.$m, '建行价');

            $currentSheet->setCellValue('AJ'.$m, '招行价');
            $currentSheet->setCellValue('AK'.$m, '招行销量');
            $currentSheet->setCellValue('AL'.$m, '招行');

            $currentSheet->setCellValue('AM'.$m, '交行价');
            $currentSheet->setCellValue('AN'.$m, '交行销量');
            $currentSheet->setCellValue('AO'.$m, '交行');

            //2017-07-10 new lines:
            $currentSheet->setCellValue('AP'.$m, '民生价');
            $currentSheet->setCellValue('AQ'.$m, '民生销量');
            $currentSheet->setCellValue('AR'.$m, '民生');

            $currentSheet->setCellValue('AS'.$m, '平安价');
            $currentSheet->setCellValue('AT'.$m, '平安销量');
            $currentSheet->setCellValue('AU'.$m, '平安');

            $currentSheet->setCellValue('AV'.$m, '中信价');
            $currentSheet->setCellValue('AW'.$m, '中信销量');
            $currentSheet->setCellValue('AX'.$m, '中信');
            //Attachment end

            $currentSheet->setCellValue('AY'.$m, '状态');

            $currentSheet->setCellValue('AZ'.$m, '备注');

            $cellIndex = 1;
            foreach ($data as $key => $value){
                $cellIndex++;
                $currentSheet->setCellValueExplicit('A'.$cellIndex,$value['oa_id'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('B'.$cellIndex,$this->returnListType($value['type']), \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('C'.$cellIndex,$value['list_name'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('D'.$cellIndex,$value['product_m'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('E'.$cellIndex,$value['code'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('F'.$cellIndex,$value['name'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('G'.$cellIndex,$value['color'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('H'.$cellIndex,$value['qty'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('I'.$cellIndex,$value['ctime'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('J'.$cellIndex,$value['pcode'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('K'.$cellIndex,$value['color'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('L'.$cellIndex,$value['maoli'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('M'.$cellIndex,$value['mall_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('N'.$cellIndex,$value['maolilv'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('O'.$cellIndex,$value['purchase_price'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('P'.$cellIndex,$value['by_stages'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('Q'.$cellIndex,$value['by_stages_rate'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('R'.$cellIndex,$value['freight'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('S'.$cellIndex,$value['gift_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('T'.$cellIndex,$value['bank_commission'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('U'.$cellIndex,$value['zengzhishui'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('V'.$cellIndex,$value['packing_charge'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('W'.$cellIndex,$value['rebate'], \PHPExcel_Cell_DataType::TYPE_STRING);

                // Attachment begin
                $currentSheet->setCellValueExplicit('X'.$cellIndex,$value['tamll_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('Y'.$cellIndex,$value['tamll_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('Z'.$cellIndex,$value['tamll'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AA'.$cellIndex,$value['jd_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AB'.$cellIndex,$value['jd_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AC'.$cellIndex,$value['jd'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AD'.$cellIndex,$value['guangfa_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AE'.$cellIndex,$value['guangfa_S_AMT'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AF'.$cellIndex,$value['guangfa'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AG'.$cellIndex,$value['jianhang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AH'.$cellIndex,$value['jianhang_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AI'.$cellIndex,$value['jianhang'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AJ'.$cellIndex,$value['zhaohang_price'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AK'.$cellIndex,$value['zhaohang_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AL'.$cellIndex,$value['zhaohang'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AM'.$cellIndex,$value['jiaohang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AN'.$cellIndex,$value['jiaohang_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AO'.$cellIndex,$value['jiaohang'],\PHPExcel_Cell_DataType::TYPE_STRING);

                //2017-07-10 new lines:
                $currentSheet->setCellValueExplicit('AP'.$cellIndex,$value['minsheng_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AQ'.$cellIndex,$value['minsheng_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AR'.$cellIndex,$value['minsheng'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AS'.$cellIndex,$value['pingan_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AT'.$cellIndex,$value['pingan_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AU'.$cellIndex,$value['pingan'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AV'.$cellIndex,$value['zhongxin_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AW'.$cellIndex,$value['zhongxin_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AX'.$cellIndex,$value['zhongxin'],\PHPExcel_Cell_DataType::TYPE_STRING);

                // Attachment end

                $currentSheet->setCellValueExplicit('AY'.$cellIndex,$this->returnItemStatus($value['status']),\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AZ'.$cellIndex,$value['remark'],\PHPExcel_Cell_DataType::TYPE_STRING);

            }

            $name = uniqid().'.xls';
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $name );
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');

        }
        else{
            echo "只能导出已通知上架的清单";
        }

    }

    function exportexcelshelveslist()
    {
        $listID = $_REQUEST['listid'];
        $emp_no = $_SESSION['emp_no'];

        $shelvesListModel = M('ShelvesList');
        $result = $shelvesListModel->where("id={$listID} and emp_no='{$emp_no}'")->find();
        if(1)//if ($result['is_send']==1)
        {
            $shelvesType = $result['type'];
            $shelvesName = $result['name'];

            $detailModel = M('ShelvesDetailed');
            $data = $detailModel->where("pid='{$listID}'")->select();

            //Export to Excel:

            Vendor('Excel.PHPExcel');
            $objPHPExcel = new \PHPExcel();
            $currentSheet = $objPHPExcel->getSheet(0);
            $m = 1;
            $currentSheet->setCellValue('A'.$m, 'OA物料号');
            $currentSheet->setCellValue('B'.$m, '上架类型');
            $currentSheet->setCellValue('C'.$m, '上架清单名');
            $currentSheet->setCellValue('D'.$m, '产品经理');
            $currentSheet->setCellValue('E'.$m, 'SAP物料号');

            $currentSheet->setCellValue('F'.$m, 'SAP物料名称');
            $currentSheet->setCellValue('G'.$m, '规格');
            //$currentSheet->setCellValue('H'.$m, '平台商品代码');
            $currentSheet->setCellValue('H'.$m, '数量');
            $currentSheet->setCellValue('I'.$m, '添加时间');
            $currentSheet->setCellValue('J'.$m, '主品物料号');

            $currentSheet->setCellValue('K'.$m, '颜色');
            $currentSheet->setCellValue('L'.$m, '毛利');
            $currentSheet->setCellValue('M'.$m, '商城价');
            $currentSheet->setCellValue('N'.$m, '毛利率');
            $currentSheet->setCellValue('O'.$m, '采购价');

            $currentSheet->setCellValue('P'.$m, '分期数');
            $currentSheet->setCellValue('Q'.$m, '分期费率(%)');
            $currentSheet->setCellValue('R'.$m, '预估运费');
            $currentSheet->setCellValue('S'.$m, '赠品价格');
            $currentSheet->setCellValue('T'.$m, '佣金');

            $currentSheet->setCellValue('U'.$m, '增值税等17%');
            $currentSheet->setCellValue('V'.$m, '包装费');
            $currentSheet->setCellValue('W'.$m, '返利金额');

            // Attachment begin
            $currentSheet->setCellValue('X'.$m, '天猫价');
            $currentSheet->setCellValue('Y'.$m, '天猫销量');
            $currentSheet->setCellValue('Z'.$m, '天猫');

            $currentSheet->setCellValue('AA'.$m, '京东价');
            $currentSheet->setCellValue('AB'.$m, '京东销量');
            $currentSheet->setCellValue('AC'.$m, '京东');

            $currentSheet->setCellValue('AD'.$m, '广发价');
            $currentSheet->setCellValue('AE'.$m, '广发销量');
            $currentSheet->setCellValue('AF'.$m, '广发');

            $currentSheet->setCellValue('AG'.$m, '建行价');
            $currentSheet->setCellValue('AH'.$m, '建行销量');
            $currentSheet->setCellValue('AI'.$m, '建行价');

            $currentSheet->setCellValue('AJ'.$m, '招行价');
            $currentSheet->setCellValue('AK'.$m, '招行销量');
            $currentSheet->setCellValue('AL'.$m, '招行');

            $currentSheet->setCellValue('AM'.$m, '交行价');
            $currentSheet->setCellValue('AN'.$m, '交行销量');
            $currentSheet->setCellValue('AO'.$m, '交行');

            //2017-07-10 new lines:
            $currentSheet->setCellValue('AP'.$m, '民生价');
            $currentSheet->setCellValue('AQ'.$m, '民生销量');
            $currentSheet->setCellValue('AR'.$m, '民生');

            $currentSheet->setCellValue('AS'.$m, '平安价');
            $currentSheet->setCellValue('AT'.$m, '平安销量');
            $currentSheet->setCellValue('AU'.$m, '平安');

            $currentSheet->setCellValue('AV'.$m, '中信价');
            $currentSheet->setCellValue('AW'.$m, '中信销量');
            $currentSheet->setCellValue('AX'.$m, '中信');

            //Attachment end

            $currentSheet->setCellValue('AY'.$m, '状态');

            $currentSheet->setCellValue('AZ'.$m, '备注');

            $cellIndex = 1;
            foreach ($data as $key => $value){
                $cellIndex++;
                $currentSheet->setCellValueExplicit('A'.$cellIndex,$value['oa_id'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('B'.$cellIndex,$this->returnListType($shelvesType), \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('C'.$cellIndex,$shelvesName, \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('D'.$cellIndex,$emp_no, \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('E'.$cellIndex,$value['code'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('F'.$cellIndex,$value['name'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('G'.$cellIndex,$value['color'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('H'.$cellIndex,$value['qty'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('I'.$cellIndex,$value['ctime'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('J'.$cellIndex,$value['pcode'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('K'.$cellIndex,$value['color'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('L'.$cellIndex,$value['maoli'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('M'.$cellIndex,$value['mall_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('N'.$cellIndex,$value['maolilv'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('O'.$cellIndex,$value['purchase_price'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('P'.$cellIndex,$value['by_stages'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('Q'.$cellIndex,$value['by_stages_rate'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('R'.$cellIndex,$value['freight'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('S'.$cellIndex,$value['gift_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('T'.$cellIndex,$value['bank_commission'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('U'.$cellIndex,$value['zengzhishui'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('V'.$cellIndex,$value['packing_charge'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('W'.$cellIndex,$value['rebate'], \PHPExcel_Cell_DataType::TYPE_STRING);

                // Attachment begin
                $currentSheet->setCellValueExplicit('X'.$cellIndex,$value['tamll_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('Y'.$cellIndex,$value['tamll_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('Z'.$cellIndex,$value['tamll'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AA'.$cellIndex,$value['jd_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AB'.$cellIndex,$value['jd_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AC'.$cellIndex,$value['jd'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AD'.$cellIndex,$value['guangfa_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AE'.$cellIndex,$value['guangfa_S_AMT'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AF'.$cellIndex,$value['guangfa'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AG'.$cellIndex,$value['jianhang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AH'.$cellIndex,$value['jianhang_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AI'.$cellIndex,$value['jianhang'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AJ'.$cellIndex,$value['zhaohang_price'],\PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AK'.$cellIndex,$value['zhaohang_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AL'.$cellIndex,$value['zhaohang'], \PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AM'.$cellIndex,$value['jiaohang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AN'.$cellIndex,$value['jiaohang_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AO'.$cellIndex,$value['jiaohang'],\PHPExcel_Cell_DataType::TYPE_STRING);

                //2017-07-10 new lines:
                $currentSheet->setCellValueExplicit('AP'.$cellIndex,$value['minsheng_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AQ'.$cellIndex,$value['minsheng_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AR'.$cellIndex,$value['minsheng'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AS'.$cellIndex,$value['pingan_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AT'.$cellIndex,$value['pingan_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AU'.$cellIndex,$value['pingan'],\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AV'.$cellIndex,$value['zhongxin_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AW'.$cellIndex,$value['zhongxin_S_AMT'], \PHPExcel_Cell_DataType::TYPE_STRING);
                $currentSheet->setCellValueExplicit('AX'.$cellIndex,$value['zhongxin'],\PHPExcel_Cell_DataType::TYPE_STRING);

                // Attachment end
                $currentSheet->setCellValueExplicit('AY'.$cellIndex,$this->returnItemStatus($value['status']),\PHPExcel_Cell_DataType::TYPE_STRING);

                $currentSheet->setCellValueExplicit('AZ'.$cellIndex,$value['remark'],\PHPExcel_Cell_DataType::TYPE_STRING);

            }

            $name = uniqid().'.xls';
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="' . $name );
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');

        }
        else{
            echo "只能导出已通知上架的清单";
        }

    }

//    function exportexcelshelveslist()
//    {
//        $listID = $_REQUEST['listid'];
//        $emp_no = $_SESSION['emp_no'];
//
//        $shelvesListModel = M('ShelvesList');
//        $result = $shelvesListModel->where("id={$listID} and emp_no='{$emp_no}'")->find();
//        if(1)//if ($result['is_send']==1)
//        {
//            $shelvesType = $result['type'];
//            $shelvesName = $result['name'];
//
//            $detailModel = M('ShelvesDetailed');
//            $data = $detailModel->where("pid='{$listID}'")->select();
//
//            //Export to Excel:
//
//            Vendor('Excel.PHPExcel');
//            $objPHPExcel = new \PHPExcel();
//            $currentSheet = $objPHPExcel->getSheet(0);
//            $m = 1;
//            $currentSheet->setCellValue('A'.$m, 'OA物料号');
//            $currentSheet->setCellValue('B'.$m, '上架类型');
//            $currentSheet->setCellValue('C'.$m, '上架清单名');
//            $currentSheet->setCellValue('D'.$m, '产品经理');
//            $currentSheet->setCellValue('E'.$m, 'SAP物料号');
//
//            $currentSheet->setCellValue('F'.$m, 'SAP物料名称');
//            $currentSheet->setCellValue('G'.$m, '规格');
//            $currentSheet->setCellValue('H'.$m, '平台商品代码');
//            $currentSheet->setCellValue('I'.$m, '添加时间');
//            $currentSheet->setCellValue('J'.$m, '主品物料号');
//
//            $currentSheet->setCellValue('K'.$m, '颜色');
//            $currentSheet->setCellValue('L'.$m, '毛利');
//            $currentSheet->setCellValue('M'.$m, '商城价');
//            $currentSheet->setCellValue('N'.$m, '毛利率');
//            $currentSheet->setCellValue('O'.$m, '采购价');
//
//            $currentSheet->setCellValue('P'.$m, '分期数');
//            $currentSheet->setCellValue('Q'.$m, '分期费率(%)');
//            $currentSheet->setCellValue('R'.$m, '预估运费');
//            $currentSheet->setCellValue('S'.$m, '赠品价格');
//            $currentSheet->setCellValue('T'.$m, '佣金');
//
//            $currentSheet->setCellValue('U'.$m, '增值税等17%');
//            $currentSheet->setCellValue('V'.$m, '包装费');
//            $currentSheet->setCellValue('W'.$m, '返利金额');
//            $currentSheet->setCellValue('X'.$m, '天猫价');
//            $currentSheet->setCellValue('Y'.$m, '天猫');
//
//            $currentSheet->setCellValue('Z'.$m, '京东价');
//            $currentSheet->setCellValue('AA'.$m, '京东');
//            $currentSheet->setCellValue('AB'.$m, '广发价');
//            $currentSheet->setCellValue('AC'.$m, '广发');
//            $currentSheet->setCellValue('AD'.$m, '建行价');
//
//            $currentSheet->setCellValue('AE'.$m, '建行');
//            $currentSheet->setCellValue('AF'.$m, '招行价');
//            $currentSheet->setCellValue('AG'.$m, '招行');
//            $currentSheet->setCellValue('AH'.$m, '交行价');
//            $currentSheet->setCellValue('AI'.$m, '交行');
//
//            $currentSheet->setCellValue('AJ'.$m, '民生价');
//            $currentSheet->setCellValue('AK'.$m, '民生');
//            $currentSheet->setCellValue('AL'.$m, '平安价');
//            $currentSheet->setCellValue('AM'.$m, '平安');
//            $currentSheet->setCellValue('AN'.$m, '中信价');
//
//            $currentSheet->setCellValue('AO'.$m, '中信');
//
//            $cellIndex = 1;
//            foreach ($data as $key => $value){
//                $cellIndex++;
//                $currentSheet->setCellValueExplicit('A'.$cellIndex,$value['oa_id'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('B'.$cellIndex,$shelvesType, \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('C'.$cellIndex,$shelvesName, \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('D'.$cellIndex,$emp_no, \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('E'.$cellIndex,$value['code'],\PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('F'.$cellIndex,$value['name'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('G'.$cellIndex,$value['color'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('H'.$cellIndex,$value['BANK_ItemCode'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('I'.$cellIndex,$value['ctime'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('J'.$cellIndex,$value['pcode'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('K'.$cellIndex,$value['color'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('L'.$cellIndex,$value['maoli'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('M'.$cellIndex,$value['mall_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('N'.$cellIndex,$value['maolilv'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('O'.$cellIndex,$value['purchase_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('P'.$cellIndex,$value['by_stages'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('Q'.$cellIndex,$value['by_stages_rate'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('R'.$cellIndex,$value['freight'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('S'.$cellIndex,$value['gift_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('T'.$cellIndex,$value['bank_commission'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('U'.$cellIndex,$value['zengzhishui'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('V'.$cellIndex,$value['packing_charge'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('W'.$cellIndex,$value['rebate'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('X'.$cellIndex,$value['tamll_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('Y'.$cellIndex,$value['tamll'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('Z'.$cellIndex,$value['jd_price'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AA'.$cellIndex,$value['jd'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AB'.$cellIndex,$value['guangfa_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AC'.$cellIndex,$value['guangfa'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AD'.$cellIndex,$value['jianhang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('AE'.$cellIndex,$value['jianhang'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AF'.$cellIndex,$value['zhaohang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AG'.$cellIndex,$value['zhaohang'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AH'.$cellIndex,$value['jiaohang_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AI'.$cellIndex,$value['jiaohang'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('AJ'.$cellIndex,$value['minsheng_price'],\PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AK'.$cellIndex,$value['minsheng'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AL'.$cellIndex,$value['pingan_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AM'.$cellIndex,$value['pingan'], \PHPExcel_Cell_DataType::TYPE_STRING);
//                $currentSheet->setCellValueExplicit('AN'.$cellIndex,$value['zhongxin_price'], \PHPExcel_Cell_DataType::TYPE_STRING);
//
//                $currentSheet->setCellValueExplicit('AO'.$cellIndex,$value['zhongxin'],\PHPExcel_Cell_DataType::TYPE_STRING);
//
//            }
//
//            $name = uniqid().'.xls';
//            header('Content-Type: application/vnd.ms-excel');
//            header('Content-Disposition: attachment;filename="' . $name );
//            header('Cache-Control: max-age=0');
//            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
//            $objWriter->save('php://output');
//
//        }
//        else{
//            echo "只能导出已通知上架的清单";
//        }
//
//    }

    function shelveslist()
    {
        $key = I('keyword');
        $map = [];
        if ($key) {
            $map['name'] = array('like', '%' . trim($key) . '%');
            $this->assign('keyword', $key);
        }
        $map['emp_no|auth'] = array('like', '%' . session('emp_no') . '%');
        $model = D("ShelvesList");
        $res = $this->_list($model, $map, 'id desc');
        $this->display();

    }


    /**
     * 从上架通知中获取物料
     */
    function getItem()
    {
        $code = I('code');
        $shelvesListName = I('shelves_list_name');
        $itemInfo = I('itemInfo');

        $shelvesListModel = M('ShelvesDetailed');
        $emp_no = session('emp_no');

        //SAP_ItemCode : SAP中的物料号(GT)
        //$quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.* from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id where list.is_send=1 and list.u_code = '{$code}' and detailed.status = '0' and list.to_emp like '%{$emp_no}%'";
        //2017-06-26 通过"上架清单名称"($shelvesListName)筛选，必须完全匹配，同时不再判断 detailed.status
        //2017-07-04 在清单中显示物料图
        /*if (empty($shelvesListName)) {
            $quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.*, sapitem.logo from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on detailed.oa_id = sapitem.item_id where list.is_send=1 and list.u_code = '{$code}' and list.to_emp like '%{$emp_no}%'";
        }else{
            $quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.*, sapitem.logo from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on detailed.oa_id = sapitem.item_id where list.is_send=1 and list.u_code = '{$code}' and list.name like '%{$shelvesListName}%' and list.to_emp like '%{$emp_no}%'";
        }
        $res = $shelvesListModel->query($quersql);*/
        //2017-07-17 添加分页
        if (empty($shelvesListName)) {
            $where = "list.is_send=1 and list.u_code = '{$code}' and list.to_emp like '%{$emp_no}%'";
        }else{
            $where = "list.is_send=1 and list.u_code = '{$code}' and list.name like '%{$shelvesListName}%' and list.to_emp like '%{$emp_no}%'";
        }
        //2017-07-18 添加物料信息过滤条件
        if(empty($itemInfo)){

        }else{
            $where .= " and CONCAT(think_shelves_detailed.name, think_shelves_detailed.code) like '%{$itemInfo}%'";
        }
        $join = 'LEFT JOIN think_shelves_list as list on think_shelves_detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on think_shelves_detailed.oa_id = sapitem.item_id';
        $filed = ("think_shelves_detailed.code as SAP_ItemCode, think_shelves_detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, think_shelves_detailed.*, sapitem.logo ");
        $pageIndex = I('pageIndex') + 1;
        $pageSize = I('pageSize');
        $_POST['p'] = $pageIndex;
        $count = $shelvesListModel->join($join)->field($filed)->where($where)->count();
        import("@.ORG.Util.Page");
        $p = new \Page($count, $pageSize);
        //分页查询数据
        $res = $shelvesListModel->join($join)->field($filed)->where($where)->limit($p->firstRow . ',' . $p->listRows)->select();

        //$res =  $shelvesListModel->where("is_send=1 and to_emp like '%{$emp_no}%' and u_code like '%{$code}%'")->select();
        $data = array('total' => $count, 'data' => $res);
        echo json_encode($data);

    }

    /**
     * 从上架通知中获取物料
     */
    function getItemForPM()
    {
        $code = I('code');
        $shelvesListName = I('shelves_list_name');
        $itemInfo = I('itemInfo');

        $shelvesListModel = M('ShelvesDetailed');
        $emp_no = session('emp_no');

        //SAP_ItemCode : SAP中的物料号(GT)
        //$quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.* from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id where list.is_send=1 and list.u_code = '{$code}' and detailed.status = '0' and list.to_emp like '%{$emp_no}%'";
        //2017-06-26 通过"上架清单名称"($shelvesListName)筛选，必须完全匹配，同时不再判断 detailed.status
        //2017-07-04 在清单中显示物料图
        /*if (empty($shelvesListName)) {
            $quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.*, sapitem.logo from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on detailed.oa_id = sapitem.item_id where list.is_send=1 and list.u_code = '{$code}' and list.to_emp like '%{$emp_no}%'";
        }else{
            $quersql = "SELECT detailed.code as SAP_ItemCode, detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, detailed.*, sapitem.logo from  think_shelves_detailed as detailed LEFT JOIN think_shelves_list as list on detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on detailed.oa_id = sapitem.item_id where list.is_send=1 and list.u_code = '{$code}' and list.name like '%{$shelvesListName}%' and list.to_emp like '%{$emp_no}%'";
        }
        $res = $shelvesListModel->query($quersql);*/
        //2017-07-17 添加分页
        if (empty($shelvesListName)) {
            $where = "list.is_send=1 and list.u_code = '{$code}' and list.emp_no = '{$emp_no}'";
        }else{
            $where = "list.is_send=1 and list.u_code = '{$code}' and list.name like '%{$shelvesListName}%' and list.emp_no = '{$emp_no}'";
        }
        //2017-07-18 添加物料信息过滤条件
        if(empty($itemInfo)){

        }else{
            $where .= " and CONCAT(think_shelves_detailed.name, think_shelves_detailed.code) like '%{$itemInfo}%'";
        }
        $join = 'LEFT JOIN think_shelves_list as list on think_shelves_detailed.pid=list.id LEFT JOIN think_sap_items as sapitem on think_shelves_detailed.oa_id = sapitem.item_id';
        $filed = ("think_shelves_detailed.code as SAP_ItemCode, think_shelves_detailed.name as SAP_ItemName, list.type, list.emp_no as product_m, list.id as list_id, list.name as list_name, think_shelves_detailed.*, sapitem.logo ");
        $pageIndex = I('pageIndex') + 1;
        $pageSize = I('pageSize');
        $_POST['p'] = $pageIndex;
        $count = $shelvesListModel->join($join)->field($filed)->where($where)->count();
        import("@.ORG.Util.Page");
        $p = new \Page($count, $pageSize);
        //分页查询数据
        $res = $shelvesListModel->join($join)->field($filed)->where($where)->limit($p->firstRow . ',' . $p->listRows)->select();

        //$res =  $shelvesListModel->where("is_send=1 and to_emp like '%{$emp_no}%' and u_code like '%{$code}%'")->select();
        $data = array('total' => $count, 'data' => $res);
        echo json_encode($data);

    }

    /**
     * 清单详情
     */
    function shelvesNoticeMs()
    {
        $pid = I('id');
        $this->assign('id', $pid);
        $this->display();
    }

    public function upadteDetailed()
    {
        $data = I('data');
        $data = json_decode($data, 1);
        unset($data['_id'], $data['_uid'], $data['_state']);
        echo ShelvesViewModel::updateDetailedAll($data);
        exit;
    }

    public function selectTags()
    {
        $list = UTagModel::getTags();
        $this->assign('list', $list);
        $this->display();
    }

    function setTag()
    {
        $model = $this->M2('product');
        $ids = I('ids');
        $tags = I('tags');
        if ($ids && $tags) {
            echo $model->where("id in({$ids})")->save(array('keywords' => $tags));
        }
        echo false;
    }

    private function M2($tableName)
    {

        return M($tableName, 'mallbuilder_', 'DB_CONFIG2');
    }

    function addtag()
    {
        $tagName = I('name');
        if ($tagName) {
            echo UTagModel::addTag($tagName);
            exit;
        }

        echo false;
    }

    function saveauth()
    {
        $users = I('users');
        echo ShelvesViewModel::updateAuthByUser($users);
        exit;
    }

    function getauthList()
    {
        $emp = session('emp_no');
        $mode = M('ShelvesAuth');
        $authUsers = $mode->where("emp_no='{$emp}'")->find();
        echo $authUsers['auth'];
    }

    // 预上架
    function preonline()
    {
        $respone = array('code' => 0, 'msg' => '');
        $postData = json_decode(I('data'), 1);

        $list = $postData['data'];
        $msg = '操作成功<br/>';
        foreach ($list as $key => $val) {
            $val['Customer_Code'] = $postData['Customer_Code'];
            unset($val['check']);
            $res = $this->updatelistforpreonline($val);
            if ($res['code']) {
                $msg .= $res['msg'] . '<br/>';
            }
        }
        $respone['msg'] = $msg;
        echo json_encode($respone, JSON_UNESCAPED_UNICODE);
        exit;
    }

    function updatelistforpreonline($rowData)
    {
        $tb_time = date('Y-m-d H:i:s');

        // 逐条更新数据
        $modelShelvesDetailed = M("ShelvesDetailed");
        $where = " 1 = 1 ";
        $where .= "and pid =  '{$rowData['pid']}' and code = '{$rowData['SAP_ItemCode']}' and oa_id = '{$rowData['oa_id']}' and id = {$rowData['id']}";
        $shelvesDetailedSaveData = array();
        $shelvesDetailedSaveData['status'] = 2; //状态: 0默认/驳回 1通知发出 2提报 3上架 4下架
        $shelvesDetailedSaveData['tb_time'] = $tb_time;
        $emp_no = $_SESSION['emp_no'];
        $shelvesDetailedSaveData['operatorname'] = $emp_no;
        $modelShelvesDetailed->where($where)->save($shelvesDetailedSaveData);

    }

    // 驳回
    function rejectthem()
    {
        $respone = array('code' => 0, 'msg' => '');
        $postData = json_decode(I('data'), 1);

        $list = $postData['data'];
        $msg = '操作成功<br/>';
        foreach ($list as $key => $val) {
            $val['Customer_Code'] = $postData['Customer_Code'];
            unset($val['check']);
            $res = $this->updatelistforreject($val);
            if ($res['code']) {
                $msg .= $res['msg'] . '<br/>';
            }
        }
        $respone['msg'] = $msg;
        echo json_encode($respone, JSON_UNESCAPED_UNICODE);
        exit;
    }

    function updatelistforreject($rowData)
    {
        $tb_time = date('Y-m-d H:i:s');

        // 逐条更新数据
        $modelShelvesDetailed = M("ShelvesDetailed");
        $where = " 1 = 1 ";
        $where .= "and pid =  '{$rowData['pid']}' and code = '{$rowData['SAP_ItemCode']}' and oa_id = '{$rowData['oa_id']}' and id = {$rowData['id']}";
        $shelvesDetailedSaveData = array();
        $shelvesDetailedSaveData['status'] = 0; //状态: 0默认/驳回 1通知发出 2提报 3上架 4下架
        $emp_no = $_SESSION['emp_no'];
        $shelvesDetailedSaveData['operatorname'] = $emp_no;
        $modelShelvesDetailed->where($where)->save($shelvesDetailedSaveData);
    }

    public function poplist()
    {
        $this->display();
    }

    public function listuser()
    {
        $key = I('key');
        $userModel = M('User');
        $where = "1 = 1 ";

        $pageIndex = I('pageIndex');//?intval(I('pageIndex'))-1:0;
        $pageSize = I('pageSize')?:50;

        if($key)
        {
            $where .= "and emp_no like '%{$key}%'";
            $result = $userModel->where($where)->order("emp_no desc")->select();//->limit($pageIndex*$pageSize,$pageSize)
        }
        else{
            $result = $userModel->order("emp_no desc")->select();//->limit($pageIndex*$pageSize,$pageSize)
        }
        $total =$userModel->where($where)->count();
        $data = array('total'=>$total,'data'=>$result);
        echo    json_encode($data,JSON_UNESCAPED_UNICODE);

    }

}